<?php
/**
* List of Users control
* @author: trungdt
* @since: Jan 25, 2011
* @version: 1.0
* 
* Change logs
* Jan 25, 2011 - trungdt - First create
*/
    if (!defined('EXPONENT')) exit('');
    require_once(BASE."subsystems/forms/controls/formcontrol.php");

    /**
    * Userscontrol Class
    *
    * @package Subsystems
    * @subpackage Forms
    */
    class userscontrol extends formcontrol {
        public $width=100;
        public $height=150;
        function name() { return "Ajax Image List Control"; }
        function isSimpleControl() { return true; }
        function getFieldDefinition() {
            return array(
            DB_FIELD_TYPE=>DB_DEF_STRING,
            DB_FIELD_LEN=>250,
            );
        }
        

        function userscontrol($current_value=-1) {
            $this->current_userid=$current_value;
        }

        function onRegister(&$form) {
            // we dont need this enctype
            //$form->enctype = "multipart/form-data";
        }

        function controlToHTML($name) {
            $html='';
            global $db, $user;
            // get file name
            $data=null;
            if (exponent_users_isAdmin())
            {
                $data=$db->selectObjectsBySql(
                "SELECT {$db->prefix}user.id id, {$db->prefix}user.username username,
                        {$db->prefix}user.firstname firstname, {$db->prefix}user.lastname lastname,
                        {$db->prefix}user.email email 
                FROM {$db->prefix}user 
                WHERE id NOT IN (
                        SELECT user_id FROM {$db->prefix}pastors
                    )"
                );
            }
            else
            {
                $data=array();
            }
            $this->id_name="usercontrol_".rand(100,9000);
            $html="<select name='{$name}' id='{$this->id_name}'>";
            if ($this->current_userid != -1)
            {
                $temp=$db->selectObject("user","id = {$this->current_userid}");
                $html.="<option value='{$this->current_userid}'>{$temp->username} - {$temp->email} - {$temp->firstname} {$temp->lastname}</option>";
                $html.="<option disabled>--------------</option>";
            }
            foreach ($data as $key=>$value)
            {
                $html.="<option value='{$value->id}'>{$value->username} - {$value->email} - {$value->firstname} {$value->lastname}</option>";
            }
            $html.="</select>";
            return $html;
        }

        function form($object) {
            if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
            exponent_forms_initialize();

            $form = new form();
            if (!isset($object->identifier)) {
                $object->identifier = "";
                $object->caption = "";
                $object->default = "";
            }
            $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

            $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
            $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
            $form->register("default",$i18n['default'], new textcontrol($object->default));
            $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
            return $form;
        }

        function update($values, $object) {
            if ($object == null) $object = new uploadcontrol();
            if ($values['identifier'] == "") {
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                $post = $_POST;
                $post['_formError'] = $i18n['id_req'];
                exponent_sessions_set("last_POST",$post);
                return null;
            }
            $object->identifier = $values['identifier'];
            $object->caption = $values['caption'];
            $object->default = $values['default'];
            return $object;
        }

        function parseData($original_name,$formvalues) {
            $file = $formvalues[$original_name];
            return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
        }
    }

?>
